#include "medialist.h"
#include <QCoreApplication>

medialist::medialist(uint number,  QObject *parent) :
    QObject(parent)
{
    //m_pMainWnd = parent;

    m_isClosePlay = false;
    m_nCur = 0;
    m_nNumber = number;

    m_pMPlayer = new mplayer((QWidget *)parent);
    connect(m_pMPlayer, SIGNAL(mplayerTimePos(float)), this, SIGNAL(mplayerTimePos(float)));
    connect(m_pMPlayer, SIGNAL(mediaPlayEnd()), this, SIGNAL(mediaPlayEnd()));

    m_isPlayFinish = true;
}

medialist::~medialist()
{
    if(m_pMPlayer != NULL)
    {
        m_pMPlayer->stop();
        m_pMPlayer->closePlay();
        delete m_pMPlayer;        
    }
//    delete m_playTimer;
}


void medialist::initList()
{
    m_fileList.clear();
    m_playTimeList.clear();
}

void medialist::getCurTimePos()
{
    if (m_pMPlayer)
    {
        m_pMPlayer->getCurTimePos();
    }
}

void medialist::seekToTimePos(const float seconds)
{
    if (m_pMPlayer)
    {
        m_pMPlayer->seekToTimePos(seconds);
    }
}

void medialist::updateList(const RoutePkgStruct &routePkg)
{

    m_fileList.clear();
    m_playTimeList.clear();

    m_pMPlayer->setrect(QRect(routePkg.layoutStruct.x,
                             routePkg.layoutStruct.y,
                             routePkg.layoutStruct.width,
                             routePkg.layoutStruct.height));


//    qDebug() << "routePkg:" << routePkg.layoutStruct.x << routePkg.layoutStruct.y << routePkg.layoutStruct.width << routePkg.layoutStruct.height;
     for(int i=0; i<routePkg.contentStructList.count(); i++)
     {
         layoutList.append(routePkg.layoutStruct);
         m_fileList.append(routePkg.contentStructList.at(i).contentFile);
         m_playTimeList.append(routePkg.contentStructList.at(i).duration);
     }


     m_nCur = 0;


     QString fullPath(g_installPath);
     fullPath.append("/");
     fullPath.append(m_fileList.first());

     //ivan @2011.10.09 add
     fullPath.replace("\\", "/");

     if (!m_pMPlayer->play(fullPath))
     {
         m_pMPlayer->play(fullPath);
     }


     m_isPlayFinish = false;
}

void medialist::nextToPlay(const RoutePkgStruct& routePkg)
{
    m_pMPlayer->setrect(QRect(routePkg.layoutStruct.x,
                             routePkg.layoutStruct.y,
                             routePkg.layoutStruct.width,
                             routePkg.layoutStruct.height));

    m_fileList.clear();
    m_playTimeList.clear();
    for(int i=0; i<routePkg.contentStructList.count(); i++)
    {
        layoutList.append(routePkg.layoutStruct);
        m_fileList.append(routePkg.contentStructList.at(i).contentFile);
        m_playTimeList.append(routePkg.contentStructList.at(i).duration);
    }

    m_nCur = 0;
    QString fullPath(g_installPath);
    fullPath.append("/");
    fullPath.append(m_fileList.first());
    //ivan @2011.10.09 add
    fullPath.replace("\\", "/");

    if (!m_pMPlayer->play(fullPath))
    {
        m_pMPlayer->play(fullPath);
    }

    m_isPlayFinish = false;
}

bool medialist::isRunning()
{
    return m_pMPlayer->isRunning();
}

bool medialist::isStop()
{
    return m_isPlayFinish;
}

void medialist::stop()
{
    m_pMPlayer->stop();
    m_isPlayFinish = true;
}

  //离开，关闭播放
void medialist::closePlay()
{
    m_isClosePlay = true;
    m_pMPlayer->stop();
    //每次须先关闭正在播放的视频,不然会影响下一个播放的视频
    m_pMPlayer->close();
//    m_pMPlayer->hide();
}

void medialist::medialistfinish()
{
    if (m_isClosePlay)
    {
        m_isClosePlay = false;
        return;
    }
//    qDebug() << "==================== medialistfinish =====================" << m_nCur;

    //如果视频播放完成，但实际要播放的时间还没结束，那么再播放
    m_nCur++;
    if(m_nCur >= m_fileList.count())
    {
        emit msgArrive(2, 3);
        m_nCur = 0;
    }


    m_pMPlayer->setrect(QRect(layoutList.at(m_nCur).x,
                              layoutList.at(m_nCur).y,
                              layoutList.at(m_nCur).width,
                              layoutList.at(m_nCur).height));

    QString fullPath(g_installPath);
    fullPath.append("/");
    fullPath.append(m_fileList.at(m_nCur));

    //ivan @2011.10.09 add
    fullPath.replace("\\", "/");

    if (!m_pMPlayer->play(fullPath))
    {
        m_pMPlayer->play(fullPath);
    }



    m_isPlayFinish = false;

}

//播放时间结束，如果切换下一个视频播放，同时把播放完成信号发送出去
void medialist::timeToStop()
{
    if(m_nCur >= m_fileList.count())
    {
        emit msgArrive(2, 3);
        m_nCur = 0;
        m_isPlayFinish = true;
    }

    QString fullPath(g_installPath);
    fullPath.append("/");
    fullPath.append(m_fileList.at(m_nCur));

    //ivan @2011.10.09 add
    fullPath.replace("\\", "/");

    m_pMPlayer->play(fullPath);
}



void medialist::hide()
{
    m_pMPlayer->hide();
}

void medialist::show()
{
    m_pMPlayer->show();
}
